package com.agricultural.robot;

import com.agricultural.robot.command.CommandInvoker;
import com.agricultural.robot.command.StartCommand;
import com.agricultural.robot.command.StopCommand;
import com.agricultural.robot.controller.PathPlanner;
import com.agricultural.robot.controller.SystemManager;
import com.agricultural.robot.controller.TaskController;
import com.agricultural.robot.factory.AgriculturalMachineFactory;
import com.agricultural.robot.factory.ConcreteAgriculturalMachineFactory;
import com.agricultural.robot.model.*;
import com.agricultural.robot.observer.FarmlandMonitor;
import com.agricultural.robot.chain.FaultDetectionHandler;
import com.agricultural.robot.chain.BoundaryProtectionHandler;
import com.agricultural.robot.chain.ObstacleAvoidanceHandler;
import com.agricultural.robot.chain.SafetyHandler;
import com.agricultural.robot.state.IdleState;
import com.agricultural.robot.strategy.NormalPathStrategy;

public class AgriculturalRobotSystem {
    public static void main(String[] args) {
        // 初始化系统
        SystemManager systemManager = new SystemManager();
        systemManager.initializeSystem();

        // 创建农田监测器 - 观察者模式
        FarmlandMonitor farmlandMonitor = new FarmlandMonitor();

        // 创建路径规划器 - 策略模式
        PathPlanner pathPlanner = new PathPlanner();
        pathPlanner.setStrategy(new NormalPathStrategy());

        // 创建任务控制器
        TaskController taskController = new TaskController();

        // 创建农机设备工厂 - 工厂方法模式
        AgriculturalMachineFactory factory = new ConcreteAgriculturalMachineFactory();

        // 创建农机设备
        AgriculturalMachine planter = factory.createMachine(MachineType.PLANTER);
        AgriculturalMachine sprayer = factory.createMachine(MachineType.SPRAYER);
        AgriculturalMachine harvester = factory.createMachine(MachineType.HARVESTER);

        // 将农机设备注册到观察者列表 - 观察者模式
        farmlandMonitor.registerObserver(planter);
        farmlandMonitor.registerObserver(sprayer);
        farmlandMonitor.registerObserver(harvester);

        // 设置农机设备的初始状态 - 状态模式
        planter.setState(new IdleState());
        sprayer.setState(new IdleState());
        harvester.setState(new IdleState());

        // 设置路径规划策略 - 策略模式
        planter.setPathStrategy(new NormalPathStrategy());
        sprayer.setPathStrategy(new NormalPathStrategy());
        harvester.setPathStrategy(new NormalPathStrategy());

        // 创建安全处理责任链 - 责任链模式
        SafetyHandler obstacleHandler = new ObstacleAvoidanceHandler();
        SafetyHandler boundaryHandler = new BoundaryProtectionHandler();
        SafetyHandler faultHandler = new FaultDetectionHandler();

        obstacleHandler.setNextHandler(boundaryHandler);
        boundaryHandler.setNextHandler(faultHandler);

        // 创建命令调用者 - 命令模式
        CommandInvoker commandInvoker = new CommandInvoker();

        // 模拟任务分配
        System.out.println("\n=== 任务分配 ===");
        taskController.assignTask(planter, new PlantingTask("区域A", "小麦"));
        taskController.assignTask(sprayer, new SprayingTask("区域B", "除草剂"));
        taskController.assignTask(harvester, new HarvestingTask("区域C", "玉米"));

        // 模拟环境变化 - 观察者模式
        System.out.println("\n=== 环境变化通知 ===");
        farmlandMonitor.setSoilMoisture(75); // 湿度过高
        farmlandMonitor.setTemperature(30);  // 温度上升
        farmlandMonitor.setCropGrowthStatus("成熟"); // 作物成熟
        farmlandMonitor.notifyObservers();

        // 模拟农机启动 - 命令模式
        System.out.println("\n=== 农机启动 ===");
        commandInvoker.executeCommand(new StartCommand(planter));
        commandInvoker.executeCommand(new StartCommand(sprayer));
        commandInvoker.executeCommand(new StartCommand(harvester));

        // 模拟障碍物检测 - 责任链模式
        System.out.println("\n=== 障碍物检测 ===");
        obstacleHandler.handleSafetyEvent(new SafetyEvent(SafetyEventType.OBSTACLE_DETECTED, "前方发现障碍物"));

        // 模拟设备故障 - 责任链模式
        System.out.println("\n=== 设备故障检测 ===");
        faultHandler.handleSafetyEvent(new SafetyEvent(SafetyEventType.DEVICE_FAULT, "发动机温度过高"));

        // 模拟农机停止 - 命令模式
        System.out.println("\n=== 农机停止 ===");
        commandInvoker.executeCommand(new StopCommand(planter));
        commandInvoker.executeCommand(new StopCommand(sprayer));
        commandInvoker.executeCommand(new StopCommand(harvester));

        // 监控系统健康状态
        System.out.println("\n=== 系统健康状态 ===");
        systemManager.monitorSystemHealth();

        // 关闭系统
        System.out.println("\n=== 系统关闭 ===");
        systemManager.shutdownSystem();
    }
}
